﻿Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.CompilerServices
Imports Stickam_Client.My
Imports System
Imports System.Diagnostics
Imports System.Net
Imports System.Runtime.CompilerServices

Namespace Stickam_Client
    Public Class StickamHttp
        ' Events
        Public Custom Event HostIDRetrieved As HostIDRetrievedEventHandler
        Public Custom Event LoggedIn As LoggedInEventHandler
        Public Custom Event RoomIPRetrieved As RoomIPRetrievedEventHandler
        Public Custom Event RoomlistRetrieved As RoomlistRetrievedEventHandler

        ' Methods
        Public Sub New()
            Me.HttpSocket = New SocketConnector
            Me.Busy = False
            Me.LastLoginData = New String(8  - 1) {}
            Me.PlayerDomain = "player.stickam.com"
            Me.StickamDomain = "www.stickam.com"
        End Sub

        Private Sub DoConnect(ByVal DomainName As String)
            Me.LastData = ""
            Dim iP As String = (Dns.GetHostAddresses(DomainName)(0).ToString & ":80")
            If MySettingsProperty.Settings.HttpEnabled Then
                iP = MySettingsProperty.Settings.HttpIP
            End If
            Me.HttpSocket.Connect(iP, MySettingsProperty.Settings.SocksIP, MySettingsProperty.Settings.SocksVersion, MySettingsProperty.Settings.SocksHTTP)
        End Sub

        Public Sub GetHostID(ByVal StickamURL As String)
            Me.Busy = True
            Me.LastURL = StickamURL
            Me.Mode = 5
            Me.DoConnect(Me.StickamDomain)
        End Sub

        Public Sub GetRoomIP(ByVal RoomGet As String, ByVal RoomType As String)
            If Not Me.Busy Then
                Me.Busy = True
                Me.LastRoomGet = RoomGet
                If (Conversions.ToDouble(RoomType) = 1) Then
                    Me.Mode = 3
                Else
                    Me.Mode = 4
                End If
                Me.DoConnect(Me.PlayerDomain)
            Else
                Dim roomIPRetrievedEvent As RoomIPRetrievedEventHandler = Me.RoomIPRetrievedEvent
                If (Not roomIPRetrievedEvent Is Nothing) Then
                    roomIPRetrievedEvent.Invoke(False, "")
                End If
            End If
        End Sub

        Public Sub GetRoomlist()
            If Not Me.Busy Then
                Me.Busy = True
                Me.Mode = 2
                Me.DoConnect(Me.StickamDomain)
            Else
                Dim roomlistRetrievedEvent As RoomlistRetrievedEventHandler = Me.RoomlistRetrievedEvent
                If (Not roomlistRetrievedEvent Is Nothing) Then
                    roomlistRetrievedEvent.Invoke("[{}]", False)
                End If
            End If
        End Sub

        Private Sub HttpSocket_Closed()
            Dim roomIPRetrievedEvent As RoomIPRetrievedEventHandler
            Select Case Me.Mode
                Case 1
                    Dim loggedInEvent As LoggedInEventHandler
                    Me.Busy = False
                    If (Strings.InStr(Me.LastData, "responseCode=0", CompareMethod.Binary) = 0) Then
                        Me.LastLoginData = New String(8  - 1) {}
                        loggedInEvent = Me.LoggedInEvent
                        If (Not loggedInEvent Is Nothing) Then
                            loggedInEvent.Invoke(Me.LastLoginData, False)
                        End If
                        Exit Select
                    End If
                    Me.LastLoginData(2) = ""
                    Me.LastLoginData(3) = Globals.GetParam("jsessionid", Me.LastData, "&")
                    Me.LastLoginData(4) = Me.LastLoginData(3)
                    Me.LastLoginData(5) = ""
                    Me.LastLoginData(6) = ""
                    Me.LastLoginData(7) = Globals.GetParam("userId", Me.LastData, "&")
                    loggedInEvent = Me.LoggedInEvent
                    If (Not loggedInEvent Is Nothing) Then
                        loggedInEvent.Invoke(Me.LastLoginData, True)
                    End If
                    Exit Select
                Case 2
                    Dim roomlistRetrievedEvent As RoomlistRetrievedEventHandler
                    Me.Busy = False
                    If (Strings.InStr(Me.LastData, "200 OK", CompareMethod.Binary) <> 10) Then
                        roomlistRetrievedEvent = Me.RoomlistRetrievedEvent
                        If (Not roomlistRetrievedEvent Is Nothing) Then
                            roomlistRetrievedEvent.Invoke("[{}]", False)
                        End If
                        Exit Select
                    End If
                    Me.LastData = Strings.Mid(Me.LastData, (Strings.InStr(Me.LastData, "roomList = ", CompareMethod.Binary) + 11))
                    roomlistRetrievedEvent = Me.RoomlistRetrievedEvent
                    If (Not roomlistRetrievedEvent Is Nothing) Then
                        roomlistRetrievedEvent.Invoke(Strings.Mid(Me.LastData, 1, (Strings.InStr(Me.LastData, "}];" & ChrW(13) & ChrW(10), CompareMethod.Binary) + 3)), True)
                    End If
                    Exit Select
                Case 3
                    Me.Busy = False
                    If (Strings.InStr(Me.LastData, "serverIP=", CompareMethod.Binary) = 0) Then
                        roomIPRetrievedEvent = Me.RoomIPRetrievedEvent
                        If (Not roomIPRetrievedEvent Is Nothing) Then
                            roomIPRetrievedEvent.Invoke(False, "")
                        End If
                        Exit Select
                    End If
                    roomIPRetrievedEvent = Me.RoomIPRetrievedEvent
                    If (Not roomIPRetrievedEvent Is Nothing) Then
                        roomIPRetrievedEvent.Invoke(True, Globals.GetParam("serverIP", Me.LastData, "&"))
                    End If
                    Exit Select
                Case 4
                    Me.Busy = False
                    If (Strings.InStr(Me.LastData, "errorCode=0", CompareMethod.Binary) = 0) Then
                        roomIPRetrievedEvent = Me.RoomIPRetrievedEvent
                        If (Not roomIPRetrievedEvent Is Nothing) Then
                            roomIPRetrievedEvent.Invoke(False, "")
                        End If
                        Exit Select
                    End If
                    roomIPRetrievedEvent = Me.RoomIPRetrievedEvent
                    If (Not roomIPRetrievedEvent Is Nothing) Then
                        roomIPRetrievedEvent.Invoke(True, (Globals.GetParam("serverIP", Me.LastData, "&") & "-" & Globals.GetParam("channelID", Me.LastData, "&")))
                    End If
                    Exit Select
                Case 5
                    Dim hostIDRetrievedEvent As HostIDRetrievedEventHandler
                    Me.Busy = False
                    If (Strings.InStr(Me.LastData, "The user was not found.", CompareMethod.Binary) <> 0) Then
                        hostIDRetrievedEvent = Me.HostIDRetrievedEvent
                        If (Not hostIDRetrievedEvent Is Nothing) Then
                            hostIDRetrievedEvent.Invoke(False, "")
                        End If
                        Exit Select
                    End If
                    Me.LastData = Strings.Mid(Me.LastData, (Strings.InStr(Me.LastData, "new ProfileUpdater(", CompareMethod.Binary) + &H13))
                    hostIDRetrievedEvent = Me.HostIDRetrievedEvent
                    If (Not hostIDRetrievedEvent Is Nothing) Then
                        hostIDRetrievedEvent.Invoke(True, Strings.Mid(Me.LastData, 1, (Strings.InStr(Me.LastData, ")", CompareMethod.Binary) - 1)))
                    End If
                    Exit Select
            End Select
        End Sub

        Private Sub HttpSocket_Connected()
            Select Case Me.Mode
                Case 1
                    Me.HttpSocket.SendData(Globals.HttpGet(Me.StickamDomain, String.Concat(New String() { "/servlet/flash/authenticateUser?j_username=", Me.LastLoginData(0), "&j_password=", Me.LastLoginData(1), "&type=99998" }), ""))
                    Exit Select
                Case 2
                    Me.HttpSocket.SendData(Globals.HttpGet(Me.StickamDomain, "/chatRoom.do", ""))
                    Exit Select
                Case 3
                    Me.HttpSocket.SendData(Globals.HttpGet(Me.PlayerDomain, ("/servlet/flash/getRoomServer?roomID=" & Me.LastRoomGet & "&type=chatroom"), ""))
                    Exit Select
                Case 4
                    Me.HttpSocket.SendData(Globals.HttpGet(Me.PlayerDomain, ("/servlet/flash/getChannel?performerID=" & Me.LastRoomGet & "&isEvent=false&type=join&hostid=" & Me.LastRoomGet), ""))
                    Exit Select
                Case 5
                    Me.HttpSocket.SendData(Globals.HttpGet(Me.StickamDomain, ("/" & Me.LastURL), ""))
                    Exit Select
            End Select
        End Sub

        Private Sub HttpSocket_ConnectFail()
            Me.Busy = False
            Select Case Me.Mode
                Case 1
                    Me.LastLoginData = New String(8  - 1) {}
                    Dim loggedInEvent As LoggedInEventHandler = Me.LoggedInEvent
                    If (Not loggedInEvent Is Nothing) Then
                        loggedInEvent.Invoke(Me.LastLoginData, False)
                    End If
                    Exit Select
                Case 2
                    Dim roomlistRetrievedEvent As RoomlistRetrievedEventHandler = Me.RoomlistRetrievedEvent
                    If (Not roomlistRetrievedEvent Is Nothing) Then
                        roomlistRetrievedEvent.Invoke("[{}]", False)
                    End If
                    Exit Select
                Case 3
                    Dim roomIPRetrievedEvent As RoomIPRetrievedEventHandler = Me.RoomIPRetrievedEvent
                    If (Not roomIPRetrievedEvent Is Nothing) Then
                        roomIPRetrievedEvent.Invoke(False, "")
                    End If
                    Exit Select
            End Select
        End Sub

        Private Sub HttpSocket_DataArrival(ByVal Data As String)
            Me.LastData = (Me.LastData & Data)
        End Sub

        Public Sub Login(ByVal Email As String, ByVal Pass As String)
            If Not Me.Busy Then
                Me.Busy = True
                Me.LastLoginData(0) = Email
                Me.LastLoginData(1) = Pass
                Me.LoginStep = 1
                Me.Mode = 1
                Me.DoConnect(Me.StickamDomain)
            Else
                Me.LastLoginData = New String(8  - 1) {}
                Dim loggedInEvent As LoggedInEventHandler = Me.LoggedInEvent
                If (Not loggedInEvent Is Nothing) Then
                    loggedInEvent.Invoke(Me.LastLoginData, False)
                End If
            End If
        End Sub


        ' Properties
        Private Overridable Property HttpSocket As SocketConnector
            <DebuggerNonUserCode> _
            Get
                Return Me._HttpSocket
            End Get
            <MethodImpl(MethodImplOptions.Synchronized), DebuggerNonUserCode> _
            Set(ByVal WithEventsValue As SocketConnector)
                Dim handler As DataArrivalEventHandler = New DataArrivalEventHandler(AddressOf Me.HttpSocket_DataArrival)
                Dim handler2 As ConnectedEventHandler = New ConnectedEventHandler(AddressOf Me.HttpSocket_Connected)
                Dim handler3 As ConnectFailEventHandler = New ConnectFailEventHandler(AddressOf Me.HttpSocket_ConnectFail)
                Dim handler4 As ClosedEventHandler = New ClosedEventHandler(AddressOf Me.HttpSocket_Closed)
                If (Not Me._HttpSocket Is Nothing) Then
                    RemoveHandler Me._HttpSocket.DataArrival, handler
                    RemoveHandler Me._HttpSocket.Connected, handler2
                    RemoveHandler Me._HttpSocket.ConnectFail, handler3
                    RemoveHandler Me._HttpSocket.Closed, handler4
                End If
                Me._HttpSocket = WithEventsValue
                If (Not Me._HttpSocket Is Nothing) Then
                    AddHandler Me._HttpSocket.DataArrival, handler
                    AddHandler Me._HttpSocket.Connected, handler2
                    AddHandler Me._HttpSocket.ConnectFail, handler3
                    AddHandler Me._HttpSocket.Closed, handler4
                End If
            End Set
        End Property


        ' Fields
        <AccessedThroughProperty("HttpSocket")> _
        Private _HttpSocket As SocketConnector
        Private Busy As Boolean
        Private LastData As String
        Private LastLoginData As String()
        Private LastRoomGet As String
        Private LastURL As String
        Private LoginStep As Integer
        Private Mode As Integer
        Private PlayerDomain As String
        Private StickamDomain As String

        ' Nested Types
        Public Delegate Sub HostIDRetrievedEventHandler(ByVal IsSuccess As Boolean, ByVal HostID As String)

        Public Delegate Sub LoggedInEventHandler(ByVal LoginData As String(), ByVal IsSuccess As Boolean)

        Public Delegate Sub RoomIPRetrievedEventHandler(ByVal IsSuccess As Boolean, ByVal RoomIP As String)

        Public Delegate Sub RoomlistRetrievedEventHandler(ByVal RoomlistData As String, ByVal IsSuccess As Boolean)
    End Class
End Namespace

